<?php 
require_once (__DIR__ . "/../../models/Addresses.php");
require_once (__DIR__ . "/../../models/Keywords.php");
require_once (__DIR__ . "/../../models/Users.php");

if(isset($_POST['id']))
{
	$row = $_POST;
	$response = Addresses::Validate($row);
	if ($response === true)
		$response = Addresses::Update($row);
	if($response === true)
	{
		header("Location: $rootUrl/../W/Addresses/index");
		die();
	}
}else{
	$row = Addresses::Get($_REQUEST['id']);
}

?>
			
				<?php if(isset($response)): ?>
					<dl class="dl-horizontal error">
						<?php foreach ($response as $key => $value): ?>
							<dt><?php echo $key?></dt>
							<dd><?php echo $value?></dd>
						<?php endforeach; ?>						
					</dl>
				<?php endif; ?>
				<form class="form-horizontal" action="" method="post">
					<input type="hidden" name="id" value="<?php echo $row['id']?>" />
					<div class="control-group">
						<label class="control-label" for="addressType">Address Type:</label>
						<div class="controls">
<?php $results = Keywords::GetChildren('addressType'); 
							echo '<select name="addressType">';
	  while($row2 = $results->fetch_assoc()): 
			if ($row2['id'] == $row['addressType'])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['name'].'</option>';
			else 
				echo '<option value="'.$row2['id'].'" >'.$row2['name'].'</option>';					
	  endwhile;	
	   						echo '</select>';

						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="line_1">Line 1:</label>
						<div class="controls">
							<input type="text" name="line_1" id="line_1" value="<?php echo $row['line_1']?>" 
					   	class="<?php echo isset($response['line_1']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['line_1'])): ?>
									<span class="error"><?php echo $response['line_1']?></span>
								<?php endif; ?>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="line_2">Line 2:</label>
						<div class="controls">
							<input type="text" name="line_2" id="line_2" value="<?php echo $row['line_2']?>" 
					   	class="<?php echo isset($response['line_2']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['line_2'])): ?>
									<span class="error"><?php echo $response['line_2']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="city">City:</label>
						<div class="controls">
							<input type="text" name="city" value="<?php echo $row['city']?>" 
					   	class="<?php echo isset($response['city']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['city'])): ?>
									<span class="error"><?php echo $response['city']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="state">State:</label>
						<div class="controls">
							<input type="text" name="state" value="<?php echo $row['state']?>" 
					   	class="<?php echo isset($response['state']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['state'])): ?>
									<span class="error"><?php echo $response['state']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="zip">Zip code:</label>
						<div class="controls">
							<input type="text" name="zip" value="<?php echo $row['zip']?>" 
					   	class="<?php echo isset($response['zip']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['zip'])): ?>
									<span class="error"><?php echo $response['zip']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="userId">User Id:</label>
						<div class="controls">
<?php $results = Users::GetAll(); 
							echo '<select name="userId">';
	  while($row2 = $results->fetch_assoc()): 
			if ($row2['id'] == $row['userId'])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['firstName'].' '.$row2['lastName'].'</option>';
			else 
				echo '<option value="'.$row2['id'].'" >'.$row2['firstName'].' '.$row2['lastName'].'</option>';					
	  endwhile;	
	   						echo '</select>';

						?> 
						</div>
					</div>
					<div class="control-group">
						<div class="controls">
							<input type="submit" value="Save" class="btn btn-primary" />
						</div>
					</div>
			
				</form>